# -*- coding: utf-8 -*-
"""
Created on Mon May 18 18:42:10 2020

@author: zhryyshr
"""
# In[]
import re
import requests
import time
from bs4 import BeautifulSoup
import os
os.chdir(r"D:\迅雷下载\唯美女生")
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}

# In[]
#第一版
def savepict(url):
    html = requests.get(url,headers = header)
    soup = BeautifulSoup(html.text, 'lxml')
    #提取链接
    label_a = soup.find_all('a')
    link_all = []
    for item in label_a:
        link_all.append(item.get('href'))
    Pictlink = []
    for item in link_all:
        aaa = re.findall('https:\/\/static\.vmgirls\.com\/image\/[\S]*\.[\S]*g',str(item))
        Pictlink = Pictlink + aaa
    #第二种标签
    print(Pictlink)
    if len(Pictlink)==0 : 
        label_a = soup.find_all('img')
        link_all = []
        Name = []
        for item in label_a:
            link_all.append(item.get('data-src'))
            Name.append(item.get('alt'))
        Pictlink = []
        for item in link_all:
            aaa = re.findall('https:\/\/static\.vmgirls\.com\/image\/[\S]*\.[\S]*g',str(item))
            Pictlink = Pictlink + aaa
    print(Pictlink)
    #下载图片保存
    for i in range(len(Pictlink)):
        time.sleep(3)
        Pict = requests.get(Pictlink[i],headers = header)
        with open(str(int(time.mktime(time.localtime())))+str('.jpg'), "wb") as file:  # wb是写二进制
            file.write(Pict.content)
# In[]

savedir = 'D:\\迅雷下载\\唯美女生\\'
url = "https://www.vmgirls.com/12812.html"
savepict(url,savedir)
# In[]
#第二版
urlmap = "https://www.vmgirls.com/photography"
maphtml = requests.get(urlmap,headers = header)
mapsoup = BeautifulSoup(maphtml.text, 'lxml')
labela = mapsoup.find_all('a')
mapname = []
maplink = []
for item in labela:
    mapname.append(item.get('title'))
    maplink.append(item.get('href'))
# In[]
def savepict(url):
    html = requests.get(url,headers = header)
    soup = BeautifulSoup(html.text, 'lxml')
    #提取链接
    label_a = soup.find_all('a')
    link_all = []
    for item in label_a:
        link_all.append(item.get('href'))
    Pictlink = []
    for item in link_all:
        aaa = re.findall('https:\/\/static\.vmgirls\.com\/image\/[\S]*\.[\S]*g',str(item))
        Pictlink = Pictlink + aaa
    #第二种标签
    print(Pictlink)
    if len(Pictlink)==0 : 
        label_a = soup.find_all('img')
        link_all = []
        Name = []
        for item in label_a:
            link_all.append(item.get('data-src'))
            Name.append(item.get('alt'))
        Pictlink = []
        for item in link_all:
            aaa = re.findall('https:\/\/static\.vmgirls\.com\/image\/[\S]*\.[\S]*g',str(item))
            Pictlink = Pictlink + aaa
        print(Pictlink)
    #下载图片保存
    for i in range(len(Pictlink)):
        time.sleep(3)
        Pict = requests.get(Pictlink[i],headers = header)
        with open(str(int(time.mktime(time.localtime())))+str('.jpg'), "wb") as file:  # wb是写二进制
            file.write(Pict.content)
# In[]
savepict('https://www.vmgirls.com/12353.html')

# In[]
import numpy as np
for i in np.arange(17,32,2):
    savepict(maplink[i])